package test.file;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.util.MultiValueMap;

public class DeleteUnsedFilesWriter implements ReportLogicalWriter {
	@Override
	public void write(MultiValueMap<String, String> info, String encoding) throws Exception {
		System.out.println("DeleteUnsedFilesWriter >> write");
		Iterator<String> keyIt = info.keySet().iterator();
		List<String> needDeleteFiles = new ArrayList<String>();
		while (keyIt.hasNext()) {
			String fileFullPath = (String) keyIt.next();
//			System.err.println(fileFullPath);
//			new Path(fileFullPath), new LinkO);
			boolean exists = Files.exists(Paths.get(fileFullPath),LinkOption.NOFOLLOW_LINKS);
			if(!exists){
				throw new Exception(fileFullPath+" not exists !");
			}
			needDeleteFiles.add(fileFullPath);
		}
		
		for (String file : needDeleteFiles) {
			Files.delete(Paths.get(file));
		}
	}
}
